package com.wdhcr.controller.admin;

import com.wdhcr.entity.Type;
import com.wdhcr.enums.ResultCode;
import com.wdhcr.service.TypeService;
import com.wdhcr.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

/*
 * @description: 分类信息的contrller层
 * @author: wdhcr
 * @dateTime: 2020/3/19 21:07
 */
@RestController
public class TypeController {

    @Autowired
    private TypeService typeService;

    /*
     * @description: 添加分类信息的接口
     * @params: [type]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/23 20:27
     */
    @CrossOrigin
    @PostMapping("/saveType")
    public ResponseResult saveType(@RequestBody Type type){
        if (type.getName() == null||type.getName() == ""){
            return new ResponseResult(ResultCode.NOT_FOUND.getCode(),"分类信息不能为空！",null);
        }
        Type typeByName = typeService.getTypeByName(type.getName());
        if (typeByName != null) {
            return new ResponseResult(ResultCode.INTERNAL_SERVER_ERROR.getCode(),"该分类已经存在！",null);
        }

        Type type1 = typeService.saveType(type);
        if (type1 != null){

                return new ResponseResult(ResultCode.SUCCESS.getCode(),"添加分类成功！",type1);
            }

        return new ResponseResult(ResultCode.INTERNAL_SERVER_ERROR.getCode(),"保存分类信息失败！",null);
    }

    /*
     * @description: 根据id查询分类信息的接口
     * @params: [id]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/23 21:42
     */
    @CrossOrigin
    @GetMapping("getTypeById/{id}")
    public ResponseResult getTypeById(@PathVariable Long id){
        Type type = typeService.getType(id);
        if (type != null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功！",type);
        }
        return new ResponseResult(ResultCode.FAIL.getCode(),"获取失败！",null);
    }

    /*
     * @description:根据id删除对应分类
     * @params: [id]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/23 21:46
     */
    @CrossOrigin
    @GetMapping("deleteTypeById/{id}")
    public ResponseResult deleteTypeById(@PathVariable Long id) throws Exception {
        try {
            typeService.deleteType(id);

                return new ResponseResult(ResultCode.SUCCESS.getCode(),"删除成功！",null);

        }catch (Exception e){
            throw  new Exception("删除失败,当前分类有关联博客！");
        }

    }

    /*
     * @description: 前端分页查询的条数的接口
     * @params: [page, size]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/21 21:14
     */
    @CrossOrigin
    @GetMapping("getTypeList/{page}/{size}")
    public ResponseResult getPageTypeList(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        //根据更新时间倒叙排列
        Sort update_time = Sort.by(Sort.Direction.DESC,"id");
        PageRequest pageRequest = PageRequest.of(page, size,update_time);
        Page<Type> all = typeService.listType(pageRequest);
        if (!all.isEmpty()){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取列表成功！",all);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"暂无分类信息！",all);

    }





}
